<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-12
 * Time: 9:09
 */

namespace app\common\logic;

use app\common\logic\Logic;
use app\common\model\Info;
use app\common\model\InfoData;
use app\common\model\InfoPic;
use app\common\model\Comment;
use think\config;

use app\common\model\Category;

class InfoLogic extends Logic
{
    protected $modelClass= 'app\common\model\Info';
    protected $column = 'title,city_id,id,create_time,user_id,catid';
    
    
    public function getList($params){
        if(isset($params['catid'])){
            $cat = Category::get($params['catid']);
            $child = Category::all(['parentid'=>$cat->id]);
            $catArr = [];
            foreach ($child as $category){
                $catArr[] = $category->id;
            }
            //一级
            if($cat->parentid==0){
                //用所有的子类组成in查询，换掉直接用一级大类的查询方法
                $params['catid'] = array('in',$catArr);
                $this->setSql($params);
                $lists = $this->prepareDataProvider();
                return $lists;
            }
        }
        
        return parent::getList($params);
    }
    
    
    /**
     * @param $params
     * @return mixed
     * 新增资讯的评论
     */
    public function addComment($params){
        $post = [
            'mid'=>2,//同城2
            'itemid'=>$params['id'],
            'user_id'=>$params['user_id'],
            'content' =>$params['content']
        ];
        $comment = new Comment();
        $comment->save($post);
        
        return callsuccess($comment->id);
    }
    

    
}